package com.novaed.game.frames;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.novaed.game.backend.ImagePanel;
import com.novaed.game.backend.Paths;
import com.novaed.game.backend.Player;
import com.novaed.game.backend.Profile;
import com.novaed.game.lifeline.TurtleTimer;

/*
 * rename to WindowMain when committing to SVN
 * */
public class MainWindow extends JFrame implements ActionListener {

	private Player player;
	private Profile online;
	private static TurtleTimer start;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		start = new TurtleTimer("STARTER");
		start.run();
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {

		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		setIconImage(new ImageIcon("./res/imgs/icon.png").getImage());
		setSize(FRAME_HEIGHT, FRAME_WIDTH);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		JPanel jp = new JPanel();
		CardLayout deck = new CardLayout(0, 0);
		
		jp.setLayout(deck);
//		jp.add(initHome(), "Menu");
		jp.add(initPlay(), "Play Screen");

		getContentPane().add(jp, BorderLayout.CENTER);
		
//        frame.addWindowListener(new WindowAdapter() {
//        	public void windowClosing(WindowEvent we) {
//        		doLogout();
//        	}
//        });

	}
	
	/**
	 * TODO java documentations
	 * */
	
	private JPanel initHome() {

		HOME = new ImagePanel(bg_menu);

		emb_login = new ImageIcon("./res/imgs/menu/emboss/login.png");
		emb_register = new ImageIcon("./res/imgs/menu/emboss/register.png");
		emb_board = new ImageIcon("./res/imgs/menu/emboss/board.png");
		emb_options = new ImageIcon("./res/imgs/menu/emboss/option.png");
		emb_credits = new ImageIcon("./res/imgs/menu/emboss/credits.png");
		emb_exit = new ImageIcon("./res/imgs/menu/emboss/exit.png");

		flt_login = new ImageIcon("./res/imgs/menu/flat/login.png");
		flt_register = new ImageIcon("./res/imgs/menu/flat/register.png");
		flt_board = new ImageIcon("./res/imgs/menu/flat/board.png");
		flt_options = new ImageIcon("./res/imgs/menu/flat/option.png");
		flt_credits = new ImageIcon("./res/imgs/menu/flat/credits.png");
		flt_exit = new ImageIcon("./res/imgs/menu/flat/exit.png");

		roll_login = new ImageIcon("./res/imgs/menu/roll/login.png");
		roll_register = new ImageIcon("./res/imgs/menu/roll/register.png");
		roll_board = new ImageIcon("./res/imgs/menu/roll/board.png");
		roll_options = new ImageIcon("./res/imgs/menu/roll/option.png");
		roll_credits = new ImageIcon("./res/imgs/menu/roll/credits.png");
		roll_exit = new ImageIcon("./res/imgs/menu/roll/exit.png");

		int strt_height = 5;

		GridBagLayout gbl_HOME = new GridBagLayout();
		gbl_HOME.columnWidths = new int[]{25, 0, 0};
		gbl_HOME.rowHeights = new int[]{140, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0, 0};
		gbl_HOME.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_HOME.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};

		HOME.setLayout(gbl_HOME);

		btn_Login = new JButton(emb_login);
		btn_Login.setPressedIcon(flt_login);
		btn_Login.setRolloverIcon(roll_login); 

		GridBagConstraints gbc_BTNLogin = new GridBagConstraints();
		gbc_BTNLogin.insets = new Insets(0, 0, 5, 0);
		gbc_BTNLogin.fill = GridBagConstraints.BOTH;
		gbc_BTNLogin.gridx = 1;
		gbc_BTNLogin.gridy = 1;
		HOME.add(btn_Login, gbc_BTNLogin);

		Component verticalStrut = Box.createVerticalStrut(strt_height-3);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut.gridx = 1;
		gbc_verticalStrut.gridy = 2;
		HOME.add(verticalStrut, gbc_verticalStrut);

		btn_Register = new JButton(emb_register);
		btn_Register.setPressedIcon(flt_register);
		btn_Register.setRolloverIcon(roll_register); 

		GridBagConstraints gbc_BTNRegister = new GridBagConstraints();
		gbc_BTNRegister.insets = new Insets(0, 0, 5, 0);
		gbc_BTNRegister.fill = GridBagConstraints.BOTH;
		gbc_BTNRegister.gridx = 1;
		gbc_BTNRegister.gridy = 3;
		HOME.add(btn_Register, gbc_BTNRegister);

		Component verticalStrut_1 = Box.createVerticalStrut(strt_height-3);
		GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
		gbc_verticalStrut_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_1.gridx = 1;
		gbc_verticalStrut_1.gridy = 4;
		HOME.add(verticalStrut_1, gbc_verticalStrut_1);

		btn_Board = new JButton(emb_board);
		btn_Board.setPressedIcon(flt_board);
		btn_Board.setRolloverIcon(roll_board); 

		btn_Board.setOpaque(false);
		GridBagConstraints gbc_BTNBoard = new GridBagConstraints();
		gbc_BTNBoard.insets = new Insets(0, 0, 5, 0);
		gbc_BTNBoard.fill = GridBagConstraints.BOTH;
		gbc_BTNBoard.gridx = 1;
		gbc_BTNBoard.gridy = 5;
		HOME.add(btn_Board, gbc_BTNBoard);

		Component verticalStrut_2 = Box.createVerticalStrut(strt_height);
		GridBagConstraints gbc_verticalStrut_2 = new GridBagConstraints();
		gbc_verticalStrut_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalStrut_2.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_2.gridx = 1;
		gbc_verticalStrut_2.gridy = 6;
		HOME.add(verticalStrut_2, gbc_verticalStrut_2);

		btn_Options = new JButton(emb_options);
		btn_Options.setPressedIcon(flt_options);
		btn_Options.setRolloverIcon(roll_options); 

		GridBagConstraints gbc_BTNOptions = new GridBagConstraints();
		gbc_BTNOptions.insets = new Insets(0, 0, 5, 0);
		gbc_BTNOptions.fill = GridBagConstraints.BOTH;
		gbc_BTNOptions.gridx = 1;
		gbc_BTNOptions.gridy = 7;
		HOME.add(btn_Options, gbc_BTNOptions);

		Component verticalStrut_3 = Box.createVerticalStrut(strt_height);
		GridBagConstraints gbc_verticalStrut_3 = new GridBagConstraints();
		gbc_verticalStrut_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalStrut_3.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_3.gridx = 1;
		gbc_verticalStrut_3.gridy = 8;
		HOME.add(verticalStrut_3, gbc_verticalStrut_3);

		btn_Credits = new JButton(emb_credits);
		btn_Credits.setPressedIcon(flt_credits);
		btn_Credits.setRolloverIcon(roll_credits); 

		GridBagConstraints gbc_BTNCredits = new GridBagConstraints();
		gbc_BTNCredits.fill = GridBagConstraints.HORIZONTAL;
		gbc_BTNCredits.insets = new Insets(0, 0, 5, 0);
		gbc_BTNCredits.gridx = 1;
		gbc_BTNCredits.gridy = 9;
		HOME.add(btn_Credits, gbc_BTNCredits);

		Component verticalStrut_4 = Box.createVerticalStrut(strt_height-3);
		GridBagConstraints gbc_verticalStrut_4 = new GridBagConstraints();
		gbc_verticalStrut_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalStrut_4.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_4.gridx = 1;
		gbc_verticalStrut_4.gridy = 10;
		HOME.add(verticalStrut_4, gbc_verticalStrut_4);

		btn_Exit = new JButton(emb_exit);
		btn_Exit.setPressedIcon(flt_exit);
		btn_Exit.setRolloverIcon(roll_exit); 

		GridBagConstraints gbc_BTNExit = new GridBagConstraints();
		gbc_BTNExit.fill = GridBagConstraints.BOTH;
		gbc_BTNExit.gridx = 1;
		gbc_BTNExit.gridy = 11;
		HOME.add(btn_Exit, gbc_BTNExit);

		List <JButton> buttons = new ArrayList<JButton>();
		buttons.add(btn_Login);
		buttons.add(btn_Register);
		buttons.add(btn_Board);
		buttons.add(btn_Options);
		buttons.add(btn_Credits);
		buttons.add(btn_Exit);

		for (JButton j : buttons) {
			j.setOpaque(false);
			j.setRolloverEnabled(true);
			j.setBorderPainted(false);  
			j.setFocusPainted(false);  
			j.setContentAreaFilled(false);
			j.addActionListener(this);
		}
		
		HOME.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		
		return HOME;

	}
	
	private JPanel initPlay() {
		
		lbl_Timer = new JLabel("30");
		lbl_Timer.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lbl_Timer.setName("Timer");
		
		// =========================================================================== Layout
		
		buttons = new ArrayList<JButton>();
		labels = new ArrayList<JLabel>();
		PLAY = new ImagePanel(bg_play);
		
		fifty = new ImageIcon("./res/imgs/fifty.png");
		turtle = new ImageIcon("./res/imgs/turtle.png");
		change = new ImageIcon("./res/imgs/switch.png");
		
		JPanel pnl_Levels = new JPanel();
		JPanel pnl_Choices = new JPanel();
		JPanel pnl_Statement = new JPanel();
		JPanel pnl_Timer = new JPanel();
		JPanel pnl_Lines = new JPanel();
		
		List <JPanel> panels = new ArrayList<JPanel>();
		panels.add(pnl_Levels);
		panels.add(pnl_Choices);
		panels.add(pnl_Statement);
		panels.add(pnl_Lines);
		panels.add(pnl_Timer);

		for (JPanel j : panels) {
			j.setOpaque(false);
		}
		
		GroupLayout gl_PLAY = new GroupLayout(PLAY);
		gl_PLAY.setHorizontalGroup(
			gl_PLAY.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_PLAY.createSequentialGroup()
					.addGroup(gl_PLAY.createParallelGroup(Alignment.LEADING)
						.addComponent(pnl_Choices, GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
						.addComponent(pnl_Statement, GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
						.addGroup(gl_PLAY.createSequentialGroup()
							.addContainerGap()
							.addComponent(pnl_Lines, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_PLAY.createSequentialGroup()
							.addGap(407)
							.addComponent(pnl_Timer, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)))
					.addGap(27)
					.addComponent(pnl_Levels, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		gl_PLAY.setVerticalGroup(
			gl_PLAY.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_PLAY.createSequentialGroup()
					.addGap(37)
					.addComponent(pnl_Timer, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
					.addGap(22)
					.addComponent(pnl_Lines, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
					.addGap(71)
					.addComponent(pnl_Statement, GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pnl_Choices, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))
				.addComponent(pnl_Levels, GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
		);
		pnl_Timer.setLayout(new BorderLayout(0, 0));
		
		lbl_Timer.setFont(new Font("Tahoma", Font.PLAIN, 32));
		lbl_Timer.setHorizontalAlignment(SwingConstants.CENTER);
		lbl_Timer.setDoubleBuffered(true);
		pnl_Timer.add(lbl_Timer);
		
		GridBagLayout gbl_pnl_Levels = new GridBagLayout();
		gbl_pnl_Levels.columnWidths = new int[]{0, 0};
		
		
		int lvl_rowHeight = 32;
		gbl_pnl_Levels.rowHeights = new int[]{lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, 
											  lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, 
											  lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight, lvl_rowHeight};
		gbl_pnl_Levels.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_pnl_Levels.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		pnl_Levels.setLayout(gbl_pnl_Levels);
		
		Component verticalStrut = Box.createVerticalStrut(40);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.fill = GridBagConstraints.VERTICAL;
		gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut.gridx = 0;
		gbc_verticalStrut.gridy = 0;
		pnl_Levels.add(verticalStrut, gbc_verticalStrut);
		
		lvl_FIFTEEN = new JLabel("15");
//		lvl_FIFTEEN.setBorder(new LineBorder(Color.RED, 2, true)); //TODO set only when lvl is active
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.fill = GridBagConstraints.BOTH;
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 0;
		gbc_label.gridy = 1;
		pnl_Levels.add(lvl_FIFTEEN, gbc_label);
		
		lvl_FOURTEEN = new JLabel("14");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 0);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 2;
		pnl_Levels.add(lvl_FOURTEEN, gbc_label_1);
		
		lvl_THIRTEEN = new JLabel("13");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.insets = new Insets(0, 0, 5, 0);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 3;
		pnl_Levels.add(lvl_THIRTEEN, gbc_label_2);
		
		lvl_TWELVE = new JLabel("12");
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.insets = new Insets(0, 0, 5, 0);
		gbc_label_3.gridx = 0;
		gbc_label_3.gridy = 4;
		pnl_Levels.add(lvl_TWELVE, gbc_label_3);
		
		lvl_ELEVEN = new JLabel("11");
		GridBagConstraints gbc_label_4 = new GridBagConstraints();
		gbc_label_4.insets = new Insets(0, 0, 5, 0);
		gbc_label_4.gridx = 0;
		gbc_label_4.gridy = 5;
		pnl_Levels.add(lvl_ELEVEN, gbc_label_4);
		
		lvl_TEN = new JLabel("10");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 6;
		pnl_Levels.add(lvl_TEN, gbc_lblNewLabel);
		
		lvl_NINE = new JLabel("9");
		GridBagConstraints gbc_label_5 = new GridBagConstraints();
		gbc_label_5.insets = new Insets(0, 0, 5, 0);
		gbc_label_5.gridx = 0;
		gbc_label_5.gridy = 7;
		pnl_Levels.add(lvl_NINE, gbc_label_5);
		
		lvl_EIGHT = new JLabel("8");
		GridBagConstraints gbc_label_6 = new GridBagConstraints();
		gbc_label_6.insets = new Insets(0, 0, 5, 0);
		gbc_label_6.gridx = 0;
		gbc_label_6.gridy = 8;
		pnl_Levels.add(lvl_EIGHT, gbc_label_6);
		
		lvl_SEVEN = new JLabel("7");
		GridBagConstraints gbc_label_7 = new GridBagConstraints();
		gbc_label_7.insets = new Insets(0, 0, 5, 0);
		gbc_label_7.gridx = 0;
		gbc_label_7.gridy = 9;
		pnl_Levels.add(lvl_SEVEN, gbc_label_7);
		
		lvl_SIX = new JLabel("6");
		GridBagConstraints gbc_label_8 = new GridBagConstraints();
		gbc_label_8.insets = new Insets(0, 0, 5, 0);
		gbc_label_8.gridx = 0;
		gbc_label_8.gridy = 10;
		pnl_Levels.add(lvl_SIX, gbc_label_8);
		
		lvl_FIVE = new JLabel("5");
		GridBagConstraints gbc_label_9 = new GridBagConstraints();
		gbc_label_9.insets = new Insets(0, 0, 5, 0);
		gbc_label_9.gridx = 0;
		gbc_label_9.gridy = 11;
		pnl_Levels.add(lvl_FIVE, gbc_label_9);
		
		lvl_FOUR = new JLabel("4");
		GridBagConstraints gbc_label_10 = new GridBagConstraints();
		gbc_label_10.insets = new Insets(0, 0, 5, 0);
		gbc_label_10.gridx = 0;
		gbc_label_10.gridy = 12;
		pnl_Levels.add(lvl_FOUR, gbc_label_10);
		
		lvl_THREE = new JLabel("3");
		GridBagConstraints gbc_label_11 = new GridBagConstraints();
		gbc_label_11.insets = new Insets(0, 0, 5, 0);
		gbc_label_11.gridx = 0;
		gbc_label_11.gridy = 13;
		pnl_Levels.add(lvl_THREE, gbc_label_11);
		
		lvl_TWO = new JLabel("2");
		GridBagConstraints gbc_label_12 = new GridBagConstraints();
		gbc_label_12.insets = new Insets(0, 0, 5, 0);
		gbc_label_12.gridx = 0;
		gbc_label_12.gridy = 14;
		pnl_Levels.add(lvl_TWO, gbc_label_12);
		
		lvl_ONE = new JLabel("1");
		GridBagConstraints gbc_label_13 = new GridBagConstraints();
		gbc_label_13.gridx = 0;
		gbc_label_13.gridy = 15;
		pnl_Levels.add(lvl_ONE, gbc_label_13);
		pnl_Statement.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		//TODO com-novaed-game #issue 7 XML Formatting
		lbl_Statement = new JLabel("<html>gfsfvsdfb3rbt3hjsnb3rtdjgwj3nogfjn ;gb3;bgijbnsuhdnguv <br /> fhovwirbtiuhfdlkjlevhrtbiwoeughkl ginp3tjv</html>");
		lbl_Statement.setAlignmentX(Component.CENTER_ALIGNMENT);
		pnl_Statement.add(lbl_Statement);
		
		GridBagLayout gbl_pnl_Choices = new GridBagLayout();
		gbl_pnl_Choices.columnWidths = new int[]{0, 0, 0};
		gbl_pnl_Choices.rowHeights = new int[]{63, 48, 0};
		gbl_pnl_Choices.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_pnl_Choices.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		pnl_Choices.setLayout(gbl_pnl_Choices);
		
		btn_A = new JButton("Button A");
		btn_A.setActionCommand("setA");
		GridBagConstraints gbc_button_2 = new GridBagConstraints();
		gbc_button_2.fill = GridBagConstraints.VERTICAL;
		gbc_button_2.insets = new Insets(0, 0, 5, 5);
		gbc_button_2.gridx = 0;
		gbc_button_2.gridy = 0;
		pnl_Choices.add(btn_A, gbc_button_2);
		
		btn_B = new JButton("Button B");
		btn_B.setActionCommand("setB");
		GridBagConstraints gbc_button_4 = new GridBagConstraints();
		gbc_button_4.fill = GridBagConstraints.VERTICAL;
		gbc_button_4.insets = new Insets(0, 0, 5, 0);
		gbc_button_4.gridx = 1;
		gbc_button_4.gridy = 0;
		pnl_Choices.add(btn_B, gbc_button_4);
		
		btn_C = new JButton("Button C");
		btn_C.setActionCommand("setC");
		GridBagConstraints gbc_button_3 = new GridBagConstraints();
		gbc_button_3.fill = GridBagConstraints.VERTICAL;
		gbc_button_3.insets = new Insets(0, 0, 0, 5);
		gbc_button_3.gridx = 0;
		gbc_button_3.gridy = 1;
		pnl_Choices.add(btn_C, gbc_button_3);
		
		btn_D = new JButton("Button D");
		btn_D.setActionCommand("setD");
		GridBagConstraints gbc_button_5 = new GridBagConstraints();
		gbc_button_5.fill = GridBagConstraints.VERTICAL;
		gbc_button_5.gridx = 1;
		gbc_button_5.gridy = 1;
		pnl_Choices.add(btn_D, gbc_button_5);
		
		btn_Fifty = new JButton(fifty);
		btn_Fifty.setActionCommand("Fifty");
		btn_Fifty.setName("LIFELINE");
		pnl_Lines.add(btn_Fifty);
		
		btn_Turtle = new JButton(turtle);
		btn_Turtle.setActionCommand("Turtle");
		btn_Turtle.setName("LIFELINE");
		pnl_Lines.add(btn_Turtle);
		
		btn_Switch = new JButton(change);
		btn_Switch.setActionCommand("Switch");
		btn_Switch.setName("LIFELINE");
		pnl_Lines.add(btn_Switch);
		
		PLAY.setLayout(gl_PLAY);
		
		buttons.add(btn_A);
		buttons.add(btn_B);
		buttons.add(btn_C);
		buttons.add(btn_D);
		
		buttons.add(btn_Switch);
		buttons.add(btn_Turtle);
		buttons.add(btn_Fifty);
				
		for (JButton j : buttons) {
			
			j.setRolloverEnabled(true);
			j.setBorderPainted(false);  
			j.setFocusPainted(false);  
			j.setContentAreaFilled(false);
			j.addActionListener(this);
			
			j.setForeground(Color.WHITE);
			
		}
		
		
		labels.add(lvl_FIFTEEN);
		labels.add(lvl_FOURTEEN);
		labels.add(lvl_THIRTEEN);
		labels.add(lvl_TWELVE);
		labels.add(lvl_ELEVEN);
		labels.add(lvl_TEN);
		labels.add(lvl_NINE);
		labels.add(lvl_EIGHT);
		labels.add(lvl_SEVEN);
		labels.add(lvl_SIX);
		labels.add(lvl_FIVE);
		labels.add(lvl_FOUR);
		labels.add(lvl_THREE);
		labels.add(lvl_TWO);
		labels.add(lvl_ONE);
		labels.add(lbl_Statement);
		labels.add(lbl_Timer);
		
		//FIXME com-novaed-game #issue 8 bg_play layout: level
		for (JLabel l: labels) {
			l.setForeground(Color.WHITE);
			l.setFont(new Font("Tahoma", Font.PLAIN, 14));
			l.setHorizontalAlignment(SwingConstants.CENTER);
			lbl_Statement.setFont(new Font("Tahoma", Font.PLAIN, 18));
			lbl_Timer.setFont(new Font("Tahoma", Font.PLAIN, 32));
		}
		// =========================================================================== Lifeline
		
		return PLAY;


	}
	
	private JPanel initBoard(){

		return BOARD;
	}

	public void doLogIn() {

		// =========================================================================== Layout
		JTextField txt_username;
		JPasswordField txt_password;
		JPanel login = new JPanel();

		GridBagLayout gbl_login = new GridBagLayout();
		gbl_login.columnWidths = new int[]{0, 0, 0, 0};
		gbl_login.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_login.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_login.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		login.setLayout(gbl_login);

		JLabel lbl_username = new JLabel("Username :");
		GridBagConstraints gbc_lbl_username = new GridBagConstraints();
		gbc_lbl_username.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_username.gridx = 0;
		gbc_lbl_username.gridy = 1;
		login.add(lbl_username, gbc_lbl_username);

		JLabel novaed = new JLabel("novaed-");
		GridBagConstraints gbc_novaed = new GridBagConstraints();
		gbc_novaed.insets = new Insets(0, 0, 5, 5);
		gbc_novaed.gridx = 1;
		gbc_novaed.gridy = 1;
		login.add(novaed, gbc_novaed);

		txt_username = new JTextField();
		txt_username.setColumns(10);
		GridBagConstraints gbc_txt_username = new GridBagConstraints();
		gbc_txt_username.insets = new Insets(0, 0, 5, 0);
		gbc_txt_username.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_username.gridx = 2;
		gbc_txt_username.gridy = 1;
		login.add(txt_username, gbc_txt_username);

		JLabel lbl_password = new JLabel("Password :");
		GridBagConstraints gbc_lbl_password = new GridBagConstraints();
		gbc_lbl_password.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_password.gridx = 0;
		gbc_lbl_password.gridy = 2;
		login.add(lbl_password, gbc_lbl_password);

		txt_password = new JPasswordField();
		txt_password.setColumns(10);
		GridBagConstraints gbc_txt_password = new GridBagConstraints();
		gbc_txt_password.gridwidth = 2;
		gbc_txt_password.insets = new Insets(0, 0, 5, 0);
		gbc_txt_password.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_password.gridx = 1;
		gbc_txt_password.gridy = 2;
		login.add(txt_password, gbc_txt_password);
		

		int result = JOptionPane.showConfirmDialog(null, login, 
				"Login to Game", JOptionPane.OK_CANCEL_OPTION);

		// =========================================================================== LogIn Code

		if (result == JOptionPane.OK_OPTION) {

			Node profile = null;
			Element user = null;
			
			String uname = txt_username.getText();
			String pword = String.valueOf(txt_password.getPassword());

			ArrayList<String> usernames = new ArrayList<String>();
			ArrayList<String> passwords = new ArrayList<String>();

			
			try {

				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(Paths.getUsers());

				doc.getDocumentElement().normalize();

				NodeList profilesList = doc.getElementsByTagName("profile");

				for (int i = 0; i <= profilesList.getLength()-1; i++) {

					profile = profilesList.item(i);
					user = (Element) profile;
					usernames.add(user.getElementsByTagName("username").item(0).getTextContent());
					passwords.add(user.getElementsByTagName("password").item(0).getTextContent());

					// username : password
					if (usernames.get(i).toString().equals(uname)) {
						
						if (passwords.get(i).toString().equals(pword)) {

						online = new Profile(txt_username.getText(), 
											user.getAttribute("pid"));

						System.out.println("ProfileID : " + user.getAttribute("pid"));
						System.out.println("Username : novaed" + user.getElementsByTagName("username").item(0).getTextContent());
						System.out.println("Password : " + user.getElementsByTagName("password").item(0).getTextContent());
						System.out.println("Registered to : " 
											+ user.getElementsByTagName("firstname").item(0).getTextContent()
											+ " "
											+ user.getElementsByTagName("lastname").item(0).getTextContent());
						System.out.println(online.getScoreSheet());
						System.out.println("Levels played : " + online.getRawScores());
						System.out.println("PCT: " + online.getPercentage());
						System.out.println("Number of times played : " + online.getTimesPlayed());
						System.out.println("Total score : " + online.getTotalScore());
						
						setTitle("Welcome, " + user.getElementsByTagName("firstname").item(0).getTextContent() + " !");
						
						getContentPane().removeAll();
						getContentPane().add(PLAY).setVisible(true);
						HOME.setVisible(false);
						
						break;
						
						} else {
							System.err.println("username or password error!");
							break;
						}
					} 
				}
				
			} catch (SAXException e) {
				System.err.println(e);
			} catch (IOException e) {
				System.err.println(e);
			} catch (ParserConfigurationException e) {
				System.err.println(e);
			} catch (NullPointerException e) {
				System.err.println(e);
			}
		} else {
			dispose();
			
			//FIXME found error here
		}

	}

	private void doLogout() {

		int result = JOptionPane.showConfirmDialog(this, "Really exit?", "Confirm",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);

		if (result == JOptionPane.YES_OPTION) {
			dispose();
		}
	}
		
	private int setUserAnswer(String answer) {
		
		return JOptionPane.showConfirmDialog(null, answer, 
				"Finalize Answer?", JOptionPane.YES_NO_OPTION);
		
	}
	
	public JLabel getTimerLabel() {
		return lbl_Timer;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {

		// ===================================================== Global Actions

		if (e.getSource() == btn_Exit) {
			doLogout();
		}

		if (e.getSource() == btn_Board) {
		}

		if (e.getSource() == btn_Options) {
		}
		

		// ===================================================== Main Menu 

		if (e.getSource() == btn_Login) {
			doLogIn();
		}

		if (e.getSource() == btn_Register) {
		}


		if (e.getSource() == btn_Credits) {
		}
		
		if (e.getSource() == btn_YES) {
			System.out.println("hey");
		}
		
		// ===================================================== Play Screen
		
		
		// LifeLines
		if (e.getSource() == btn_Fifty) {
			new Dialog("Do you want to use the 50/50?", btn_Fifty);
		}
		
		if (e.getSource() == btn_Turtle) {
			new Dialog("Do you want to use the Turtle Timer?", btn_Turtle);
		}
		
		if (e.getSource() == btn_Switch) {
			new Dialog("Do you want to change questions?", btn_Switch);
		}
		
		
		

	}
	


	// ========================================================	 Class Variables

	/*
	 * frame and panels
	 * */
	
	private JPanel HOME = null;
	private JPanel PLAY = null;
	private JPanel OPTION = null;
	private JPanel USR_STATS = null;
	private JPanel BOARD = null;

	// panel and frame properties
	private final int FRAME_HEIGHT = 806;
	private final int FRAME_WIDTH = 620;

	/*
	 * background images
	 * 
	 * */

	private String bg_menu = "res/imgs/menu/bg_menu.png";
	private String bg_play = "res/imgs/menu/bg_play.png";

	/*
	 * button images
	 * 
	 * */

	private ImageIcon emb_login = null;
	private ImageIcon emb_register = null;
	private ImageIcon emb_board = null;
	private ImageIcon emb_options = null;
	private ImageIcon emb_credits = null;
	private ImageIcon emb_exit = null;

	private ImageIcon flt_login = null;
	private ImageIcon flt_register = null;
	private ImageIcon flt_board = null;
	private ImageIcon flt_options = null;
	private ImageIcon flt_credits = null;
	private ImageIcon flt_exit = null;

	private ImageIcon roll_login = null;
	private ImageIcon roll_register = null;
	private ImageIcon roll_board = null;
	private ImageIcon roll_options = null;
	private ImageIcon roll_credits = null;
	private ImageIcon roll_exit = null;
	
	private ImageIcon fifty = null;
	private ImageIcon turtle = null;
	private ImageIcon change = null;

	/*
	 * buttons
	 * */
	private JButton btn_Login = null;
	private JButton btn_Register = null;
	private JButton btn_Board = null;
	private JButton btn_Options = null;
	private JButton btn_Credits = null;
	private JButton btn_Exit = null;

	private JButton btn_Play = null;
	private JButton btn_A = null;
	private JButton btn_B = null;
	private JButton btn_C = null;
	private JButton btn_D = null;
	
	private JButton btn_Fifty = null;
	private JButton btn_Turtle = null;
	private JButton btn_Switch = null;
	
	private JButton btn_YES = null;
	private JButton btn_CANCEL = null;
	
	private JLabel lvl_ONE = null;
	private JLabel lvl_TWO = null;
	private JLabel lvl_THREE = null;
	private JLabel lvl_FOUR = null;
	private JLabel lvl_FIVE = null;
	private JLabel lvl_SIX = null;
	private JLabel lvl_SEVEN = null;
	private JLabel lvl_EIGHT = null;
	private JLabel lvl_NINE = null;
	private JLabel lvl_TEN = null;
	private JLabel lvl_ELEVEN = null;
	private JLabel lvl_TWELVE = null;
	private JLabel lvl_THIRTEEN = null;
	private JLabel lvl_FOURTEEN = null;
	private JLabel lvl_FIFTEEN = null;
	
	private JLabel lbl_Statement = null;
	
	

	/**
	 * Used concurrently with TurtleTimer.class
	 * @see TurtleTimer.class
	 * */
	private static JLabel lbl_Timer = null;
	
	
	private List<JButton> buttons = null;
	private List<JLabel> labels = null;
}